package com.ui.element;

import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class HexFrame extends JFrame {

	private static final long serialVersionUID = 8561366413110653162L;

	public boolean isValidate() {
		return isVisible();
	}

	public enum NOTICE_TYPE {
		NORMAL, ERROR;
	}

	protected void alertMsg(String msg) {
		JOptionPane.showMessageDialog(null, msg);
	}

	protected void alertWran(String msg) {
		JOptionPane.showMessageDialog(null, msg, EleConsts.FONT_ARIAL, JOptionPane.WARNING_MESSAGE);
	}

	protected void alertError(String msg) {
		JOptionPane.showMessageDialog(null, msg, EleConsts.FONT_ARIAL, JOptionPane.ERROR_MESSAGE);
	}

	protected int alertYesNo(String title, String msg) {
		return JOptionPane.showConfirmDialog(null, title, msg, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
	}

	protected void showNotice(HexLabel el, String msg) {
		showNotice(el, NOTICE_TYPE.NORMAL, msg);
	}

	protected void showNotice(HexLabel el, NOTICE_TYPE isError, String msg) {
		el.setText(msg);
		if (NOTICE_TYPE.ERROR == isError) {
			el.setForeground(Color.red);
		} else {
			el.setForeground(Color.black);
		}
	}

	protected void showNotice(HexLabel el, NOTICE_TYPE isError, String msg, String... vps) {
		String str = new String(msg);
		for (int i = 0; i < vps.length; i++) {
			str = str.replaceFirst("%@", vps[i]);
		}
		showNotice(el, isError, str);
	}

	protected void setTitle(String title, String logourl) {
		super.setTitle(title);
		try {
			Image logo = ImageIO.read(this.getClass().getResource("/img/tp16.png"));
			super.setIconImage(logo);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void setSize(int w, int h) {
		double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		super.setLocation(new Point((int) (lx / 2) - w / 2, (int) (ly / 2) - h / 2));// �趨���ڳ���λ��
		super.setSize(w, h);
		super.setResizable(false);
		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}
